package com.dfbz.controller.Student;

import com.dfbz.model.Student;
import com.dfbz.service.Impl.StudentServiceimpl;
import com.dfbz.service.StudentService;
import javafx.print.Printer;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

public class ListServlet extends HttpServlet {
    private StudentService studentService = new StudentServiceimpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Student> studentList = studentService.findAll();
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        PrintWriter pw = resp.getWriter();
        pw.write("<!DOCTYPE html>\n" +
                "<html lang=\"en\">\n" +
                "<head>\n" +
                "    <meta charset=\"UTF-8\">\n" +
                "    <title>学生列表</title>\n" +
                "    <script src=\"/web02/js/jquery-1.12.4.js\"></script>\n" +
                "    <script src=\"/web02/js/学生列表.js\"></script>\n" +
                "    <style>\n" +
                "        .ins {\n" +
                "            width: 100px;\n" +
                "            height: 30px;\n" +
                "        }\n" +
                "\n" +
                "\n" +
                "        .box {\n" +
                "            width: 800px;\n" +
                "            height: 800px;\n" +
                "            margin:30px auto;\n" +
                "            text-align: center;\n" +
                "            border: 1px solid black;\n" +
                "            padding: 10px;\n" +
                "        }\n" +
                "\n" +
                "        table {\n" +
                "\n" +
                "            margin: 50px auto;\n" +
                "        }\n" +
                "\n" +
                "        table tr td {\n" +
                "            border: 1px solid black;\n" +
                "            width: 60px;\n" +
                "            height: 60px;\n" +
                "            text-align: center;\n" +
                "            line-height: 50px;\n" +
                "        }\n" +
                "        table tr th {\n" +
                "            width: 100px;\n" +
                "            height: 60px;\n" +
                "            border: 1px solid black;\n" +
                "            text-align: center;\n" +
                "            line-height: 50px;\n" +
                "        }\n" +
                "\n" +
                "        p {\n" +
                "            width: 500px;\n" +
                "            height: 50px;\n" +
                "            margin: 10px auto;\n" +
                "        }\n" +
                "\n" +
                "        p a{\n" +
                "            margin-left: 10px;\n" +
                "            float: left;\n" +
                "        }\n" +
                "\n" +
                "        p span {\n" +
                "            float: right;\n" +
                "            margin-right: 15px;\n" +
                "        }\n" +
                "\n" +
                "        tr td a {\n" +
                "            text-decoration: none;\n" +
                "\n" +
                "        }\n" +
                "\n" +
                "        tr td input[type='text'] {\n" +
                "            width: 20px;\n" +
                "            height: 20px;\n" +
                "        }\n" +
                "\n" +
                "        .active {\n" +
                "            background-color: skyblue;\n" +
                "        }\n" +
                "\n" +
                "    </style>\n" +
                "</head>\n" +
                "<body>\n" +
                "<div class=\"box\">\n" +
                "\n" +
                "    <input type=\"text\" placeholder=\"请输入ID\" class=\"id ins\">\n" +
                "    <input type=\"text\" placeholder=\"请输入名字\" class=\"name ins\">\n" +
                "    <input type=\"text\" placeholder=\"请输入手机号码\" class=\"phone ins\">\n" +
                "    <input type=\"text\" placeholder=\"请输入地址\" class=\"address ins\">\n" +
                "    <input type=\"button\" value=\"添加\" class=\"btn\">\n" +
                "\n"+
                " <table class=\"tab\">\n" +
                "        <thead>\n" +
                "        <tr>\n" +
                "            <th><input type=\"checkbox\" class=\"all\"></th>\n" +
                "            <th>ID</th>\n" +
                "            <th>姓名</th>\n" +
                "            <th>手机号码</th>\n" +
                "            <th>地址</th>\n" +
                "            <th>操作</th>\n" +
                "        </tr>\n" +
                "        </thead>\n" +
                "        <tbody>");
        pw.write("动态的部分");
          for (Student student:studentList) {
              pw.write("<tr>\n" +
                      "            <td><input type=\"checkbox\" class=\"ck\" ></td>\n" +
                      "            <td class=\"id\">"+student.getId()+"</td>\n" +
                      "            <td class=\"name\">"+student.getName()+"</td>\n" +
                      "            <td class=\"phone\">"+student.getPhone()+"</td>\n" +
                      "            <td class=\"address\">"+student.getAddress()+"</td>\n" +
                      "            <td class=\"del\"><a href=\"javascript:;\">删除</a><span>/<a href=\"javascript:;\">更新</a></span></td>\n" +
                      "        </tr>");
          }

        pw.write(  "    </tbody>\n" +
                "    </table> <p>\n" +
                "        <a href=\"javascript:;\" class=\"delRow\">删除选中学生信息</a>\n" +
                "        <br>\n" +
                "        <br>\n" +
                "        <a href=\"javascript:;\" class=\"delAll\">清空所有学生信息</a>\n" +
                "        <span class=\"amount_sum\">学生总数:<em>1</em></span>\n" +
                "    </p>\n" +
                "</div>\n" +
                "</body>\n" +
                "</html>");
    }
}
